
import 'package:flutter/material.dart';
import 'package:xvideos/assets_tool/app_colors.dart';
import 'package:xvideos/hj_model/comment/comment_model.dart';
import 'package:xvideos/routers/jump_router.dart';

enum ItemHighlightType { IS_TOP, IS_RECOMMEND, IS_CHOOSE }

class CommentRichText extends StatefulWidget {
  final CommentModel? model;

  const CommentRichText({
    Key? key,
    this.model,
  }) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _CommentRichTextState();
  }
}

class _CommentRichTextState extends State<CommentRichText> {
  CommentModel? get model => widget.model;

  bool get isRichText {
    return model?.linkStr?.isNotEmpty == true && model?.searchKeyword?.isNotEmpty == true && titleArr.length > 1;
  }

  String get richTitle => model?.searchKeyword ?? "";

  List<String> get titleArr {
    return model?.content?.split("$richTitle").toList() ?? [];
  }

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    if (isRichText) {
      return _buildContentTextPan();
    } else {
      return Text(
        "${model?.content}",
        style: TextStyle(
          color: Color(0xE5FFFFFF),
          fontSize: 14,
        ),
      );
    }
  }

  Widget _buildContentTextPan() {
    List<InlineSpan> spanArr = [];
    for (int i = 0; i < titleArr.length; i++) {
      bool isLast = i == (titleArr.length - 1);
      spanArr.add(TextSpan(text: titleArr[i]));
      if (!isLast) {
        spanArr.add(
          WidgetSpan(
            child: GestureDetector(
              onTap: () => pushToPageByLink(model?.linkStr, id: model?.id),
              child: Text(
                richTitle,
                style: TextStyle(
                  color: AppColors.primaryHighColor,
                  fontSize: 14,
                  height: 1.2,
                ),
              ),
            ),
          ),
        );
      }
    }
    return Text.rich(
      TextSpan(
        style: TextStyle(
          color: Color(0xE5FFFFFF),
          fontSize: 14,
        ),
        children: spanArr,
      ),
    );
  }
}
